package model

import (
	"gorm.io/gorm"
)

type SubmitBasic struct {
	gorm.Model
	Identity     string        `gorm:"column:identity;type:varchar(36)" json:"identity"`
	UserId       string        `gorm:"column:user_id;type:varchar(36)" json:"user_id"`
	UserBasic    *UserBasic    `gorm:"foreignKey:UserId;references:Identity"`
	ProblemId    string        `gorm:"column:problem_id;type:varchar(36)" json:"problem_id"`
	ProblemBasic *ProblemBasic `gorm:"foreignKey:ProblemId;references:Identity"`
	Path         string        `gorm:"column:path;type:text" json:"path"`
	Status       uint8         `gorm:"column:status;type:tinyint(1)" json:"status"`
}

func (table *SubmitBasic) TableName() string {
	return "submit_basic"
}

func GetSubmitList(problemIdentity, userIdentity string, status int) *gorm.DB {
	tx := DB.Model(&SubmitBasic{}).Preload("UserBasic").Preload("ProblemBasic", func(db *gorm.DB) *gorm.DB {
		return db.Omit("content")
	})
	if problemIdentity != "" {
		tx.Where("problem_id = ?", problemIdentity)
	}
	if userIdentity != "" {
		tx.Where("user_id = ?", userIdentity)
	}
	if status != 0 {
		tx.Where("status = ?", status)
	}
	return tx
}
